/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package my12306;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import my12306.http.Consts;
import my12306.ocr.OCR;
import org.jdesktop.application.Action;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Jack
 */
public class LoginFrame extends javax.swing.JFrame implements IUpdateImage {

    /**
     * Creates new form LoginFrame
     */
    private final Logger log = LoggerFactory.getLogger(getClass());
    private TrainClient trainClient;
    private final static ImageThread imageThread = My12306App.getImageThread();

    public LoginFrame() {
        initComponents();
        this.trainClient = My12306App.getTrainClient();

        labRandimage.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                imageThread.continueRun();
            }
        });
        Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation((size.width - getSize().width) / 2, (size.height - getSize().height) / 2);        
        imageThread.download(Consts.URL_passCodeAction_rand_login);
    }

    public void updateImage(byte[] image) {
        try {
            labRandimage.setIcon(new ImageIcon(image));
            labRandimage.setText(OCR.doOCR(image));
        } catch (Exception ex) {
            log.error(ex.getLocalizedMessage(), ex);
        } catch (Error ex) {
            log.error(ex.getLocalizedMessage(), ex);
        }
    }

    public TrainClient getTrainClient() {
        return this.trainClient;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtPassword = new javax.swing.JPasswordField();
        labUsername = new javax.swing.JLabel();
        btnLogin = new javax.swing.JButton();
        labPwd = new javax.swing.JLabel();
        labRandcode = new javax.swing.JLabel();
        txtUsername = new javax.swing.JTextField();
        txtRandcode = new javax.swing.JTextField();
        labRandimage = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        statusLab = new javax.swing.JLabel();
        jCheckBox1 = new javax.swing.JCheckBox();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(my12306.My12306App.class).getContext().getResourceMap(LoginFrame.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        txtPassword.setName("txtPassword"); // NOI18N

        labUsername.setText(resourceMap.getString("labUsername.text")); // NOI18N
        labUsername.setName("labUsername"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(my12306.My12306App.class).getContext().getActionMap(LoginFrame.class, this);
        btnLogin.setAction(actionMap.get("loginAction")); // NOI18N
        btnLogin.setText(resourceMap.getString("btnLogin.text")); // NOI18N
        btnLogin.setName("btnLogin"); // NOI18N

        labPwd.setText(resourceMap.getString("labPwd.text")); // NOI18N
        labPwd.setName("labPwd"); // NOI18N

        labRandcode.setText(resourceMap.getString("labRandcode.text")); // NOI18N
        labRandcode.setName("labRandcode"); // NOI18N

        txtUsername.setName("txtUsername"); // NOI18N

        txtRandcode.setName("txtRandcode"); // NOI18N
        txtRandcode.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtRandcodeKeyPressed(evt);
            }
        });

        labRandimage.setName("labRandimage"); // NOI18N

        jSeparator1.setName("jSeparator1"); // NOI18N

        statusLab.setText(resourceMap.getString("statusLab.text")); // NOI18N
        statusLab.setName("statusLab"); // NOI18N

        jCheckBox1.setSelected(true);
        jCheckBox1.setText(resourceMap.getString("jCheckBox1.text")); // NOI18N
        jCheckBox1.setName("jCheckBox1"); // NOI18N

        jLabel1.setIcon(resourceMap.getIcon("jLabel1.icon")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 344, Short.MAX_VALUE)
            .addComponent(statusLab, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(90, 90, 90)
                        .addComponent(jCheckBox1)
                        .addGap(41, 41, 41)
                        .addComponent(btnLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(labRandcode)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtRandcode, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(labRandimage, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(labPwd)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(txtPassword))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(labUsername)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(101, 101, 101)
                        .addComponent(jLabel1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(labUsername)
                            .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(labPwd)
                            .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(labRandcode)
                            .addComponent(txtRandcode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(labRandimage, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnLogin)
                    .addComponent(jCheckBox1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusLab, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    void doLogin() {
        try {
//            trainClient.loginInit();
//            trainClient.loginAction_init();
            String result = trainClient.login(txtUsername.getText(),
                    txtPassword.getText(),
                    txtRandcode.getText(),
                    trainClient.loginAction_loginAysnSuggest()).getMsg();
            statusLab.setText(result);
            if (trainClient.isLoginOk()) {
//                imageThread.setStop();
                this.setVisible(false);
//                My12306App.launch(My12306App.class, args);
                this.dispose();
                My12306App app = My12306App.getApplication();
                imageThread.setUpdator(getTrainClient());
                if (jCheckBox1.isSelected()) {
                    app.show(new My12306ViewMini(app));
                } else {
                    app.show(new My12306View(app));
                }
            } else {
                txtRandcode.setText("");
                imageThread.continueRun();
            }
        } catch (Exception ex) {
            log.error(ex.getLocalizedMessage(), ex);
        }
    }

    private void txtRandcodeKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtRandcodeKeyPressed
        if (evt.getKeyCode() == 13 || evt.getKeyCode() == 10) {
            doLogin();
        }
    }//GEN-LAST:event_txtRandcodeKeyPressed

    @Action
    public void loginAction() {
        doLogin();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnLogin;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel labPwd;
    private javax.swing.JLabel labRandcode;
    private javax.swing.JLabel labRandimage;
    private javax.swing.JLabel labUsername;
    private javax.swing.JLabel statusLab;
    private javax.swing.JPasswordField txtPassword;
    private javax.swing.JTextField txtRandcode;
    private javax.swing.JTextField txtUsername;
    // End of variables declaration//GEN-END:variables
}
